package com.huluobo.a2107mvpimdemo.demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.huluobo.a2107mvpimdemo.R;

/**
 * 补间动画演示效果,平移(translate),缩放(scale),旋转(rotate),渐变(alpha),只需要设定起始值和终点值即可,系统会自动补充中间的动画,来实现动画效果
 * 帧动画:一帧一帧的动画,需要大量的图片资源才好看,一般现在不推荐使用,因为大量图片会影响包的体积
 * 属性动画:ValueAnimator 和ObjectAnimator
 * 1.ValueAnimator ofInt() ofFloat 或者ofObject这种静态方法实现
 */
public class DemoActivity extends AppCompatActivity {

    private ImageView imageView;

    private Button translate, rotate, scale, alpha, set, valueAnim, objectAnim, objectAnimSet, Interpolator;
    private LinearLayout ll_root;

    private MyView myView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);

        imageView = findViewById(R.id.imageview);
        translate = findViewById(R.id.translate);
        rotate = findViewById(R.id.rotate);
        scale = findViewById(R.id.scale);
        alpha = findViewById(R.id.alpha);
        set = findViewById(R.id.set);
        ll_root = findViewById(R.id.ll_root);
        valueAnim = findViewById(R.id.valueAnim);
        objectAnim = findViewById(R.id.objectAnim);
        objectAnimSet = findViewById(R.id.objectAnimSet);
        Interpolator = findViewById(R.id.Interpolator);

        myView=findViewById(R.id.myView);
        myView.start(1);

        //设置接口回调,当动画结束时,回调出方法
        myView.setOnAnimEndListener(new MyView.OnAnimEndListener() {
            @Override
            public void onAnimEnd() {
                Toast.makeText(DemoActivity.this, "动画结束,可以跳转", Toast.LENGTH_SHORT).show();
            }
        });

        Animation animation_translate = AnimationUtils.loadAnimation(this, R.anim.translate_anim);
        Animation animation_rotate = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
        Animation animation_scale = AnimationUtils.loadAnimation(this, R.anim.scale_anim);
        Animation animation_alpha = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
        Animation animation_set = AnimationUtils.loadAnimation(this, R.anim.anim_set);
        translate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView.startAnimation(animation_translate);
            }
        });
        rotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView.startAnimation(animation_rotate);
            }
        });

        scale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView.startAnimation(animation_scale);
            }
        });

        alpha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView.startAnimation(animation_alpha);
            }
        });

        set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView.startAnimation(animation_set);
            }
        });

        valueAnim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //属性可以变化的动画,ofFloat 代表咱们给的属性变化的值
                //addUpdateListener 是当属性发生变化的监听器
                //animation.getAnimatedValue(); 是当前在变化中的值
                ValueAnimator valueAnimator = ValueAnimator.ofFloat(1f, 0.8f, 0.6f, 0.4f, 0.2f, 0f);
                valueAnimator.setDuration(5000);
                valueAnimator.setRepeatCount(ValueAnimator.INFINITE);//动画执行的重复次数,INFINITE代表无限
                valueAnimator.setRepeatMode(ValueAnimator.REVERSE);//重复播放的方式,reverse会反向重复,restart会重头重复
                //监听ofFloat值的变化,当值发生改变时,一定要给出赋值
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                        //取值
                        float currentValue = (float) animation.getAnimatedValue();
                        Log.i("TagA", "currentValue:" + currentValue);
                        imageView.setScaleX(currentValue);
                        imageView.setScaleY(currentValue);
                    }
                });
                valueAnimator.start();
            }
        });

        objectAnim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //alpha 渐变
                //scaleX X轴缩放
                //scaleY Y轴缩放
                //translateX X轴的平移
                //translateY Y轴的平移
                //rotation 旋转
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.1f);
                objectAnimator.setDuration(1500);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                        Log.i("TagA", "value" + animation.getAnimatedValue());
                    }
                });
                objectAnimator.start();
            }
        });

        objectAnimSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator rotate = ObjectAnimator.ofFloat(imageView, "rotation", 0, 360);
                ObjectAnimator translationX = ObjectAnimator.ofFloat(imageView, "translationX", -100, 100);
                ObjectAnimator translationY = ObjectAnimator.ofFloat(imageView, "translationY", -100, 100);
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 0, 1);
                ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 0, 1);
                ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1, 0, 1);

                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.play(rotate) //要执行的效果,必须要有一个play
                        .with(alpha)//与play同时执行
                        .with(scaleY)
                        .after(scaleX)//执行完,执行play
                        .after(1000)//将play中延迟1秒后执行
                        .before(translationX)//执行完play,再执行
                        .before(translationY);
                //当前的执行顺序 scaleX -> 隔一秒 -> rotate,alpha和scaleY -> translationX和translationY
                animatorSet.setDuration(15000);
                animatorSet.start();
            }
        });
        Interpolator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator translationY = ObjectAnimator.ofFloat(imageView, "translationY", -800, 400);
                //new AccelerateInterpolator() 加速度插值器,开始慢 中间加速
                //new DecelerateInterpolator() 减速插值器,开始快 中间开始减速
                //new AccelerateDecelerateInterpolator() 先加速后减速,开始慢 中间快 结束慢
                //new AnticipateInterpolator() 反向加速,先回后退一段距离 再加速
                //new BounceInterpolator() 弹球落地效果
                //new CycleInterpolator(2) 循环插值器
                //new LinearInterpolator() 线性插值器,匀速移动,默认
                //new OvershootInterpolator() 超越插值器,与反向加速相反,先超过终点一段距离,在返回终点
                translationY.setInterpolator(new AnticipateOvershootInterpolator());
                translationY.setDuration(5000);
                translationY.start();
            }
        });

    }
}